import { defineStore } from 'pinia'

export const userStore = defineStore('user', () => {
  const userName = localStorage.getItem('_cangku_login_warehouseUserName')
  const employeeId = localStorage.getItem('_cangku_login_warehouseUserId')
  const account = localStorage.getItem('_cangku_login_warehouseAccount')
  const token = localStorage.getItem('_cangku_token')

  const login = (employee) => {
    if (localStorage.getItem('_cangku_login_warehouseUserName')) {
      localStorage.removeItem('_cangku_login_warehouseUserName')
    }
    localStorage.setItem('_cangku_login_warehouseAccount', employee.warehouseAccount)
    localStorage.setItem('_cangku_login_warehouseUserName', employee.warehouseUserName)
    localStorage.setItem('_cangku_login_warehouseUserId', employee.warehouseUserId)
    localStorage.setItem('_cangku_token', employee.token)
  }

  const logout = () => {
    localStorage.removeItem('_cangku_token')
    localStorage.removeItem('_cangku_login_warehouseUserName')
  }

  return {
    userName,
    employeeId,
    account,
    token,
    login,
    logout
  }
})
